require 'fileutils'

$curdir = pwd
TARGETDIR = ENV['TARGET_TEMP_DIR']
raise "TARGET_TEMP_DIR is not set" if TARGETDIR.nil?
OBJDIR = ENV['TEMP_FILES_DIR']
raise "TEMP_FILES_DIR is not set" if OBJDIR.nil?
$rootdir = ENV['RHO_ROOT']
raise "RHO_ROOT is not set" if $rootdir.nil?
$appincdir = ENV["RHO_INC"]
raise "RHO_INC is not set" if $appincdir.nil?
$androidndkpath = ENV['ANDROID_NDK']
raise 'ANDROID_NDK is not set' if $androidndkpath.nil?

require File.join($rootdir, 'platform/android/build/androidcommon.rb')

ARGS = ['-Iext',
    "-I\"#{$rootdir}/lib/extensions/digest/ext\"",
    "-I\"#{$rootdir}/platform/shared/ruby/include\"",
    "-I\"#{$rootdir}/platform/shared\"",
    "-I\"#{$rootdir}/platform/shared/common\"",
    "-I\"#{$rootdir}/platform/shared/ruby/android\"",
    "-I\"#{$rootdir}/platform/shared/ruby/generated\""]

OBJDIR_ARM = File.join(OBJDIR,'arm')
OBJDIR_x86 = File.join(OBJDIR,'x86')
OBJDIR_MIPS = File.join(OBJDIR,'mips')
OBJDIR_AARCH64 = File.join(OBJDIR,'aarch64')

SRC = get_sources File.join($curdir, 'ext_native.files')

OBJ_ARM = FileList.new(get_objects(SRC, OBJDIR_ARM))
OBJ_x86 = FileList.new(get_objects(SRC, OBJDIR_x86))
OBJ_MIPS = FileList.new(get_objects(SRC, OBJDIR_MIPS))
OBJ_AARCH64 = FileList.new(get_objects(SRC, OBJDIR_AARCH64))

TARGETDIR_ARM = File.join(TARGETDIR,'armeabi')
TARGETDIR_x86 = File.join(TARGETDIR,'x86')
TARGETDIR_MIPS = File.join(TARGETDIR,'mips')
TARGETDIR_AARCH64 = File.join(TARGETDIR,'aarch64')

TARGETLIB_ARM = File.join(TARGETDIR_ARM,'libdigest-sha1.a')
TARGETLIB_x86 = File.join(TARGETDIR_x86,'libdigest-sha1.a')
TARGETLIB_MIPS = File.join(TARGETDIR_MIPS,'libdigest-sha1.a')
TARGETLIB_AARCH64 = File.join(TARGETDIR_AARCH64,'libdigest-sha1.a')

cd '../..'

namespace 'config' do
  task :arm do
      setup_ndk(ENV['ANDROID_NDK'],ENV['ANDROID_API_LEVEL'],'arm')
  end
  
  task :x86 do
      setup_ndk(ENV['ANDROID_NDK'],ENV['ANDROID_API_LEVEL'],'x86')
  end
  
  task :mips do
      setup_ndk(ENV['ANDROID_NDK'],ENV['ANDROID_API_LEVEL'],'mips')
  end

  task :aarch64 do
    setup_ndk(ENV['ANDROID_NDK'],ENV['ANDROID_API_LEVEL'],'aarch64')
  end
end

directory TARGETDIR_ARM
directory TARGETDIR_x86
directory TARGETDIR_MIPS
directory TARGETDIR_AARCH64
directory OBJDIR_ARM
directory OBJDIR_x86
directory OBJDIR_MIPS
directory OBJDIR_AARCH64

rule '.o' => lambda {|obj| find_source(obj) } do |t|
  abi = File.basename(File.dirname(t.name))
  Rake::Task["config:#{abi}"].invoke
  cc_compile find_source(t.name), File.dirname(t.name), ARGS or raise "\e[31mCompilation failed: #{find_source(t.name)}\e[0m"
end

task OBJ_ARM => OBJDIR_ARM
task OBJ_x86 => OBJDIR_x86
task OBJ_MIPS => OBJDIR_MIPS
task OBJ_AARCH64 => OBJDIR_AARCH64

def find_source(objfile)
  base = File.basename(objfile, '.o')
  src = SRC.find { |s| File.basename(s) == base }
  src
end

file TARGETLIB_ARM => TARGETDIR_ARM do |t|
    Rake::Task["config:arm"].invoke
    cc_ar t.name, OBJ_ARM or raise "\e[31mCreating library failed: #{t.name}\e[0m"
end
multitask TARGETLIB_ARM => OBJ_ARM

file TARGETLIB_x86 => TARGETDIR_x86 do |t|
    Rake::Task["config:x86"].invoke
    cc_ar t.name, OBJ_x86 or raise "\e[31mCreating library failed: #{t.name}\e[0m"
end
multitask TARGETLIB_x86 => OBJ_x86

file TARGETLIB_MIPS => TARGETDIR_MIPS do |t|
    Rake::Task["config:mips"].invoke
    cc_ar t.name, OBJ_MIPS or raise "\e[31mCreating library failed: #{t.name}\e[0m"
end
multitask TARGETLIB_MIPS => OBJ_MIPS

file TARGETLIB_AARCH64 => TARGETDIR_AARCH64 do |t|
    Rake::Task["config:aarch64"].invoke
    cc_ar t.name, OBJ_AARCH64 or raise "\e[31mCreating library failed: #{t.name}\e[0m"
end
multitask TARGETLIB_AARCH64 => OBJ_AARCH64

namespace 'arch' do
  task :arm => [TARGETLIB_ARM]
  task :x86 => [TARGETLIB_x86]
  task :mips => [TARGETLIB_MIPS]
  task :aarch64 => [TARGETLIB_AARCH64]
end

task :default => ["arch:arm", "arch:x86"]
